home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // Paint3.cpp
- //
- //***********************************************************************
-
- #define OEMRESOURCE
-
- #include <afxwin.h>
- #include <afxdlgs.h>
- #include "Resource.h"
- #include "Paint3.h"
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_PAINT ()
- ON_COMMAND (IDM_FILE_NEW, OnFileNew)
- ON_COMMAND (IDM_FILE_OPEN, OnFileOpen)
- ON_COMMAND (IDM_FILE_SAVE, OnFileSave)
- ON_COMMAND (IDM_FILE_SAVE_AS, OnFileSaveAs)
- ON_COMMAND (IDM_FILE_EXIT, OnFileExit)
- ON_COMMAND_RANGE (IDM_WIDTH_VTHIN, IDM_WIDTH_VTHICK, OnWidth)
- ON_COMMAND_RANGE (IDM_COLOR_BLACK, IDM_COLOR_WHITE, OnColor)
- ON_UPDATE_COMMAND_UI (IDM_FILE_NEW, OnUpdateFileUI)
- ON_UPDATE_COMMAND_UI (IDM_FILE_SAVE, OnUpdateFileUI)
- ON_UPDATE_COMMAND_UI (IDM_FILE_SAVE_AS, OnUpdateFileUI)
- ON_UPDATE_COMMAND_UI_RANGE (IDM_WIDTH_VTHIN, IDM_WIDTH_VTHICK,
- OnUpdateWidthUI)
- ON_UPDATE_COMMAND_UI_RANGE (IDM_COLOR_BLACK, IDM_COLOR_WHITE,
- OnUpdateColorUI)
- ON_WM_LBUTTONDOWN ()
- ON_WM_MOUSEMOVE ()
- ON_WM_LBUTTONUP ()
- ON_WM_CONTEXTMENU ()
- ON_WM_MEASUREITEM ()
- ON_WM_DRAWITEM ()
- END_MESSAGE_MAP ()
-
- const COLORREF CMainWindow::crColors[8] = {
- RGB ( 0, 0, 0), // Black
- RGB ( 0, 0, 255), // Blue
- RGB ( 0, 255, 0), // Green
- RGB ( 0, 255, 255), // Cyan
- RGB (255, 0, 0), // Red
- RGB (255, 0, 255), // Magenta
- RGB (255, 255, 0), // Yellow
- RGB (255, 255, 255) // White
- };
-
- const char CMainWindow::szFilters[] =
- "Paint Files (*.pnt)|*.pnt|All Files (*.*)|*.*||";
-
- CMainWindow::CMainWindow ()
- {
- m_nColor = IDM_COLOR_RED;
- m_nWidth = IDM_WIDTH_MEDIUM;
- m_lineArray.SetSize (0, 64);
-
- CString strWndClass = AfxRegisterWndClass (
- 0,
- myApp.LoadStandardCursor (IDC_CROSS),
- (HBRUSH) (COLOR_WINDOW + 1),
- myApp.LoadIcon (IDR_MAINFRAME)
- );
-
- Create (strWndClass, "Paint3", WS_OVERLAPPEDWINDOW,
- rectDefault, NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
-
- LoadAccelTable (MAKEINTRESOURCE (IDR_MAINFRAME));
-
- CMenu* pMenu = GetMenu ();
- for (int i=0; i<8; i++)
- pMenu->ModifyMenu (IDM_COLOR_BLACK + i, MF_BYCOMMAND |
- MF_OWNERDRAW, IDM_COLOR_BLACK + i);
- }
-
- CMainWindow::~CMainWindow ()
- {
- DeleteAllLines ();
- }
-
- void CMainWindow::OnPaint ()
- {
- CPaintDC dc (this);
- int nCount = m_lineArray.GetSize ();
-
- if (nCount) {
- for (int i=0; i<nCount; i++)
- ((CLine*) m_lineArray[i])->Draw (&dc);
- }
- }
-
- void CMainWindow::OnFileNew ()
- {
- m_strFileName.Empty ();
- m_strPathName.Empty ();
- UpdateWindowTitle ();
-
- DeleteAllLines ();
- Invalidate ();
- }
-
- void CMainWindow::OnFileOpen ()
- {
- CFileDialog dlg (TRUE, "pnt", "*.pnt",
- OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters, this);
-
- if (dlg.DoModal () == IDOK) {
- if (LoadFile (dlg.GetPathName ())) {
- m_strFileName = dlg.GetFileTitle ();
- m_strPathName = dlg.GetPathName ();
- UpdateWindowTitle ();
- }
- }
- }
-
- void CMainWindow::OnFileSave ()
- {
- if (!m_strFileName.IsEmpty ())
- SaveFile (m_strPathName);
- else // Need a file name first
- OnFileSaveAs ();
- }
-
- void CMainWindow::OnFileSaveAs ()
- {
- CFileDialog dlg (FALSE, "pnt", m_strPathName,
- OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
- szFilters, this);
-
- if (dlg.DoModal () == IDOK) {
- if (SaveFile (dlg.GetPathName ())) {
- m_strFileName = dlg.GetFileTitle ();
- m_strPathName = dlg.GetPathName ();
- UpdateWindowTitle ();
- }
- }
- }
-
- void CMainWindow::OnUpdateFileUI (CCmdUI* pCmdUI)
- {
- pCmdUI->Enable (m_lineArray.GetSize ());
- }
-
- void CMainWindow::OnFileExit ()
- {
- SendMessage (WM_CLOSE, 0, 0);
- }
-
- void CMainWindow::OnWidth (UINT nID)
- {
- m_nWidth = nID;
- }
-
- void CMainWindow::OnColor (UINT nID)
- {
- m_nColor = nID;
- }
-
- void CMainWindow::OnUpdateWidthUI (CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck (pCmdUI->m_nID == m_nWidth);
- }
-
- void CMainWindow::OnUpdateColorUI (CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck (pCmdUI->m_nID == m_nColor);
- }
-
- void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
- {
- m_ptFrom = point;
- m_ptTo = point;
- SetCapture ();
- }
-
- void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
- {
- if (GetCapture () == this) {
- CClientDC dc (this);
- InvertLine (&dc, m_ptFrom, m_ptTo);
- InvertLine (&dc, m_ptFrom, point);
- m_ptTo = point;
- }
- }
-
- void CMainWindow::OnLButtonUp (UINT nFlags, CPoint point)
- {
- static UINT nWidths[5] = { 1, 8, 16, 24, 32 };
-
- if (GetCapture () == this) {
- ReleaseCapture ();
-
- CClientDC dc (this);
- InvertLine (&dc, m_ptFrom, m_ptTo);
- CLine* pLine = NULL;
-
- try {
- pLine = new CLine (m_ptFrom, m_ptTo,
- nWidths[m_nWidth - IDM_WIDTH_VTHIN],
- crColors[m_nColor - IDM_COLOR_BLACK]);
-
- m_lineArray.Add (pLine);
- pLine->Draw (&dc);
- }
- catch (CMemoryException* e) {
- MessageBox ("Out of memory. You must clear the " \
- "drawing area before adding more lines.", "Error",
- MB_ICONEXCLAMATION | MB_OK);
-
- if (pLine != NULL)
- delete pLine;
- e->Delete ();
- }
- }
- }
-
- void CMainWindow::OnContextMenu (CWnd* pWnd, CPoint point)
- {
- CRect rect;
- GetClientRect (&rect);
- ClientToScreen (&rect);
-
- if (rect.PtInRect (point)) {
- CMenu menu;
- menu.LoadMenu (IDR_CONTEXTMENU);
- CMenu* pContextMenu = menu.GetSubMenu (0);
-
- for (int i=0; i<8; i++)
- pContextMenu->ModifyMenu (IDM_COLOR_BLACK + i,
- MF_BYCOMMAND | MF_OWNERDRAW, IDM_COLOR_BLACK + i);
-
- pContextMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON |
- TPM_RIGHTBUTTON, point.x, point.y, this);
- return;
- }
- CFrameWnd::OnContextMenu (pWnd, point);
- }
-
- void CMainWindow::OnMeasureItem (int nIDCtl, LPMEASUREITEMSTRUCT lpmis)
- {
- lpmis->itemWidth = ::GetSystemMetrics (SM_CYMENU) * 4;
- lpmis->itemHeight = ::GetSystemMetrics (SM_CYMENU);
- }
-
- void CMainWindow::OnDrawItem (int nIDCtl, LPDRAWITEMSTRUCT lpdis)
- {
- BITMAP bm;
- CBitmap bitmap;
- bitmap.LoadOEMBitmap (OBM_CHECK);
- bitmap.GetObject (sizeof (bm), &bm);
-
- CDC dc;
- dc.Attach (lpdis->hDC);
-
- CBrush* pBrush = new CBrush (::GetSysColor ((lpdis->itemState &
- ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_MENU));
- dc.FrameRect (&(lpdis->rcItem), pBrush);
- delete pBrush;
-
- if (lpdis->itemState & ODS_CHECKED) {
- CDC dcMem;
- dcMem.CreateCompatibleDC (&dc);
- CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
-
- dc.BitBlt (lpdis->rcItem.left + 4, lpdis->rcItem.top +
- (((lpdis->rcItem.bottom - lpdis->rcItem.top) -
- bm.bmHeight) / 2), bm.bmWidth, bm.bmHeight, &dcMem,
- 0, 0, SRCCOPY);
-
- dcMem.SelectObject (pOldBitmap);
- }
-
- pBrush = new CBrush (crColors[lpdis->itemID - IDM_COLOR_BLACK]);
- CRect rect = lpdis->rcItem;
- rect.DeflateRect (6, 4);
- rect.left += bm.bmWidth;
- dc.FillRect (rect, pBrush);
- delete pBrush;
-
- dc.Detach ();
- }
-
- void CMainWindow::InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo)
- {
- int nOldMode = pDC->SetROP2 (R2_NOT);
-
- pDC->MoveTo (ptFrom);
- pDC->LineTo (ptTo);
-
- pDC->SetROP2 (nOldMode);
- }
-
- void CMainWindow::DeleteAllLines ()
- {
- int nCount = m_lineArray.GetSize ();
-
- for (int i=0; i<nCount; i++)
- delete m_lineArray[i];
-
- m_lineArray.RemoveAll ();
- }
-
- BOOL CMainWindow::LoadFile (LPCSTR lpszFileName)
- {
- CFile file;
- BOOL bResult = TRUE;
-
- if (file.Open (lpszFileName, CFile::modeRead)) {
- CWaitCursor wait;
- DeleteAllLines ();
- CArchive ar (&file, CArchive::load);
- m_lineArray.Serialize (ar);
- Invalidate ();
- }
- else {
- CString string;
- string.Format ("Unable to open %s", lpszFileName);
- MessageBox (string, "Error", MB_ICONEXCLAMATION | MB_OK);
- bResult = FALSE;
- }
- return bResult;
- }
-
- BOOL CMainWindow::SaveFile (LPCSTR lpszFileName)
- {
- CFile file;
- BOOL bResult = TRUE;
-
- if (file.Open (lpszFileName, CFile::modeCreate | CFile::modeWrite)) {
- CWaitCursor wait;
- CArchive ar (&file, CArchive::store);
- m_lineArray.Serialize (ar);
- }
- else {
- CString string;
- string.Format ("Unable to create %s", lpszFileName);
- MessageBox (string, "Error", MB_ICONEXCLAMATION | MB_OK);
- bResult = FALSE;
- }
- return bResult;
- }
-
- void CMainWindow::UpdateWindowTitle ()
- {
- CString strTitle;
-
- if (m_strFileName.IsEmpty ())
- strTitle = "Paint3";
- else
- strTitle = m_strFileName + " - Paint3";
-
- SetWindowText (strTitle);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CLine member functions
-
- IMPLEMENT_SERIAL (CLine, CObject, 1)
-
- CLine::CLine (CPoint ptFrom, CPoint ptTo, UINT nWidth, COLORREF crColor)
- {
- m_ptFrom = ptFrom;
- m_ptTo = ptTo;
- m_nWidth = nWidth;
- m_crColor = crColor;
- }
-
- void CLine::Serialize (CArchive& ar)
- {
- CObject::Serialize (ar);
-
- if (ar.IsStoring ())
- ar << m_ptFrom << m_ptTo << m_nWidth << (DWORD) m_crColor;
- else
- ar >> m_ptFrom >> m_ptTo >> m_nWidth >> (DWORD) m_crColor;
- }
-
- void CLine::Draw (CDC* pDC)
- {
- CPen pen (PS_SOLID, m_nWidth, m_crColor);
-
- CPen* pOldPen = pDC->SelectObject (&pen);
- pDC->MoveTo (m_ptFrom);
- pDC->LineTo (m_ptTo);
-
- pDC->SelectObject (pOldPen);
- }
-